﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;


public partial class Control_Inventory_User_Inventory2 : System.Web.UI.UserControl
{

   // int accid = 1;
    protected void Page_Load(object sender, EventArgs e)
    {       
      
        if (!IsPostBack)
        {
            loadContent();
        }
    }

    private void loadContent()
    {
        DropDownList drp;
        try
        {
            int accid = int.Parse(Session["ACC_ID"].ToString());
            PRODUCT_TYPESDataAPI product_type = new PRODUCT_TYPESDataAPI();
            DataTable dt_type = product_type.GetPRO_TYPE_NAME_ACCID(accid);

<<<<<<< .mine

            PRODUCTSDataAPI product = new PRODUCTSDataAPI();

            int a = GVInventory.Columns.Count;
=======
                 
               PRODUCTSDataAPI product = new PRODUCTSDataAPI();
               
                int a = GVInventory.Columns.Count;
>>>>>>> .r328

            GVInventory.DataSource = dt_type;
            GVInventory.DataBind();

            for (int i = 0; i < GVInventory.Rows.Count; i++)
            {
                string id = string.Empty;
                GridViewRow rows = GVInventory.Rows[i];
                string proTypeName = rows.Cells[1].Text;
                string proTypeId = rows.Cells[0].Text;
                string index = rows.Cells[0].Text;
                GVInventory.Columns[0].Visible = false;

                PRODUCTSDataAPI product1 = new PRODUCTSDataAPI();

                DataTable dt_pro1 = product1.getByPRO_ACCID(Convert.ToInt32(index), accid);
                drp = (rows.FindControl("DDProduct") as DropDownList);
                Label lbl = rows.FindControl("Label1") as Label;
                lbl.Text = "";
                if (dt_pro1 == null)
                {
                    DataTable name = new DataTable();
                    drp.DataSource = name;
                    drp.DataTextField = "";
                    drp.DataValueField = "";
                    drp.DataBind();
                    drp.Text = "";
                    drp.Width = 120;
                    drp.Height = 30;

                }
                else
                {
                    drp.DataSource = dt_pro1;
                    drp.DataTextField = dt_pro1.Columns["PRO_NAME"].ToString();
                    drp.DataValueField = dt_pro1.Columns["PRO_QUANTITY"].ToString();
                    drp.Width = 120;
                    drp.Height = 30;
                    drp.DataBind();
                    string proid = drp.SelectedValue.ToString();                   

                    //DataTable dt_pro2 = product.getByPRO_QTTY(Convert.ToInt32(proid), accid);
                   
                    lbl.Text = dt_pro1.Rows[0][2].ToString();
                   // lbl.Text = dt_pro1.Columns["PRO_QUANTITY"].ToString();
                }

                drp.Items.Insert(0, new ListItem("== Add New Items ==", "0"));
                drp.Attributes.Add("onChange", "return changeQuantity(this,'" + lbl.ClientID + "'," + proTypeId + ",'" + proTypeName + "','" + txtProTypeName.ClientID + "','" + hdTempId.ClientID + "')");
            }
        }
        catch (Exception ex) { ex.ToString(); }
    }

    protected void addNewProduct(object sender, EventArgs e)
    {
        try
        {
            PRODUCTSDataAPI product = new PRODUCTSDataAPI();
            string name = txtProName.Text;
            string description = txtProDescription.Text;
            decimal price = decimal.Parse(txtProPrice.Text);
            int quantity = int.Parse(txtProQuantity.Text);
            int accId = Convert.ToInt32(Session["ACC_ID"]);
            int proTypeId = int.Parse(hdTempId.Value.ToString());

            //Response.Write(name + " " + description + " " + price + " " + quantity + " " + accId + " " + proTypeId);

            product.insertPRODUCTS(proTypeId, accId, name, description, price, quantity);
            //loadContent();
            Response.AppendHeader("Refresh", "0;URL=Admin_Inventory.aspx");
            
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not AddProduct')</script>");
        }
<<<<<<< .mine
=======
       
>>>>>>> .r328
    }
   
}
